# encoding: utf-8
class Admin::BanjisController < ApplicationController
  def index
    @banjis = Banji.find(:all)
    @page_title = 'Listing banjis'
  end  
 
  def create
    #debugger
    setting = Setting.where("id = ?", params[:banji][:setting_id]).first
    period_of_g1 = (setting.created_at.year.to_i + 3).to_s
    period_of_g2 = (setting.created_at.year.to_i + 2).to_s
    period_of_g3 = (setting.created_at.year.to_i + 1).to_s
    fields = [:name, :code, :setting_id]
    data = []    
    for i in 1..setting.classes_of_j1P.to_i do
      data.push([period_of_g1 + "届初一普" + i.to_s + "班","j1P" + i.to_s , setting.id])
    end
    for i in 1..setting.classes_of_j2P.to_i do
      data.push([period_of_g2 + "届初二普" + i.to_s + "班","j2P" + i.to_s , setting.id])
    end
    for i in 1..setting.classes_of_j3P.to_i do
      data.push([period_of_g3 + "届初三普" + i.to_s + "班","j3P" + i.to_s , setting.id])
    end
    for i in 1..setting.classes_of_s1P.to_i do
      data.push([period_of_g1 + "届高一普" + i.to_s + "班","s1P" + i.to_s , setting.id])
    end
    for i in 1..setting.classes_of_s2W.to_i do
      data.push([period_of_g2 + "届高二文" + i.to_s + "班","s2W" + i.to_s , setting.id])
    end
    for i in 1..setting.classes_of_s2L.to_i do
      data.push([period_of_g2 + "届高二理" + i.to_s + "班","s2L" + i.to_s , setting.id])
    end
    for i in 1..setting.classes_of_s3W.to_i do
      data.push([period_of_g3 + "届高三文" + i.to_s + "班","s3W" + i.to_s , setting.id])
    end
    for i in 1..setting.classes_of_s3L.to_i do
      data.push([period_of_g3 + "届高三理" + i.to_s + "班","s3L" + i.to_s , setting.id])
    end    
    if Banji.import fields, data
      flash[:notice] = "Classes was successfully created."
    redirect_to :action => 'index'
    else
      @page_title = 'Create new classes'
      render :action => 'new'
    end
  end

  def new
    @page_title = 'Create new class'
    @banji = Banji.new    
  end
  
  def edit
    @banji = Banji.find(params[:id])
    @page_title = 'Edit banji'
  end

  def update
    @banji = Banji.find(params[:id])
    if @banji.update_attributes(params[:banji])
      flash[:notice] = "banji #{@banji.name} was successfully updated."
    redirect_to :action => 'show', :id => @banji.id
    else
      @page_title = 'Edit banji'
      render :action => 'edit'
    end
  end

  def destroy
  end

  def show
    @banji = Banji.find(params[:id])
    @page_title = @banji.name
  end
end
